print("------------ 创建Set ------------")
// 创建Set的方式1
var letters = Set<Character>()
letters.insert("c")
print(letters)

// 创建Set的方式2
var courses: Set<String> = ["Math", "English", "History"]
courses.insert("Chemistry")
print(courses)

// 自定义类型需要实现Hashable协议
struct Person {
    var name: String
    var age: Int
}

extension Person: Hashable {
    func hash(into hasher: inout Hasher) {
        hasher.combine(name)
        hasher.combine(age)
    }
}

var personSet = Set<Person>()
personSet.insert(Person(name: "zhangsan", age: 18))
print(personSet)


// Set的遍历
print("------------ Set的遍历(Set是无序的) ------------")
for course in courses {
    print(course)
}

print("------------ 排序后再遍历 ------------")
for course in courses.sorted() {
    print(course)
}

print(courses.count)
print(courses.isEmpty)

print("------------ courses update(with:) ------------")
courses.update(with: "philogophy")
print(courses)

courses.insert("Chemistry")
print(courses)

// insert和update区别是什么???

print("------------ Set删除元素(remove) ------------")
var numbers1: Set<Int> = [1, 2, 3, 4, 5]
numbers1.remove(3)
print(numbers1)
// 因为set是无序的, 所以删除的不一样的是第一个放入元素
numbers1.removeFirst()
print(numbers1)
numbers1.removeAll()
print(numbers1)

print("------------ Set删除元素(filter) ------------")
var numbers2: Set<Int> = [1, 2, 3, 4, 5]
print(numbers2.filter { $0 != 2 })
print(numbers2)
